package com.demo2do.boyle.client.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.client.service.SocialAccountService;
import com.demo2do.boyle.common.entity.customer.Customer;
import com.demo2do.boyle.common.entity.customer.social.SocialAccount;
import com.demo2do.boyle.common.entity.customer.social.SocialType;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.service.impl.GenericServiceImpl;

/**
 * 
 * @author Wilson
 */
@Service("socialAccountService")
public class SocialAccountServiceImpl extends GenericServiceImpl<SocialAccount> implements SocialAccountService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see com.demo2do.boyle.client.service.SocialAccountService#list(com.demo2do.boyle.common.entity.customer.Customer)
     */
    @SuppressWarnings("unchecked")
    public List<SocialAccount> list(Customer customer) {

        return genericDaoSupport.searchForList("FROM SocialAccount WHERE disabled = false AND customer = :customer", "customer", customer);

    }

	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.client.service.SocialAccountService#listSocialType(com.demo2do.boyle.common.entity.customer.Customer)
	 */
	@SuppressWarnings("unchecked")
	public List<SocialType> listSocialType(Customer customer) {
		return genericDaoSupport.searchForList("SELECT type FROM SocialAccount WHERE disabled = false AND customer = :customer", "customer", customer);
	}

	@SuppressWarnings("unchecked")
	public List<SocialAccount> querySocialAccountByCustomerId(Customer customer) {
		return genericDaoSupport.searchForList("FROM SocialAccount WHERE disabled = false AND customer = :customer", "customer", customer);
	}

	@Override
	public SocialAccount querySocialAccount(Customer customer, SocialType socialType) {
		
		Map<String,Object> parameters = new HashMap<String,Object>();
		parameters.put("customer", customer);
		parameters.put("socialType", socialType);
		
		@SuppressWarnings("unchecked")
		List<SocialAccount> list = genericDaoSupport.searchForList("FROM SocialAccount WHERE disabled = false AND customer = :customer and type = :socialType",  parameters);
		
		return list.size() == 0 ? null : list.get(0);
	}

}
